# 使用带有 rt 模式的 open() 函数读取文本文件 并使用with管理上下文，确保文件被正确关闭
# Read the entire file as a single string
# with open('somefile.txt', 'rt') as f:
#     data = f.read()
#
# # Iterate over the lines of the file
# with open('somefile.txt', 'rt') as f:
#     for line in f:
# open()函数还有很多可选项，具体可学
# with open('io.txt', 'w') as f:
#     f.write('Hello World\n')
#     f.write('new content\n')
#     f.write('new content\n')
#     f.write('new content\n')
#     f.write('new content\n')
#
# with open('io.txt', 'r') as f:
#     for line in f:
#         print(line, end='')
# with open('io.txt', 'a') as f:
#     print('NEW CONTENT', file=f)
#     print('NEW CONTENT', file=f)
#     print('NEW CONTENT', file=f)  # print会自动添加换行符

# print()函数 使用 sep='' 和 end='' 参数
# print('str', 5, 3, 'acc', 3.14)
# print('str', 5, 3, 'acc', 3.14, sep=',')
# print('str', 5, 3, 'acc', 3.14, sep=',', end='!\n')
# # 有时也会看见这种写法
# print(','.join(('ACME', '50', '91.5')))
# # 问题在于join只适用于字符串
# row = ('ACME', 50, 91.5)
# print(*row, sep=',')  # 这样写更加优雅

# 尝试一下二进制文本
# with open('io.bin', 'wb') as f:
#     f.write(b'Hello World\n')
#     # print(b'Hello World', file=f)  # print()函数不可以写入二进制文件
#     f.write(b'new content\n')
#
# with open('io.bin', 'rb') as f:
#     data = f.read()
#     print(list(data))
# import array
#
# a = array.array('i', [1, 2, 3, 4, 5])
# # print(a[3])
# with open('io.bin', 'wb') as f:
#     f.write(a)
# arr = array.array('i', [1, 2])
# with open('io.bin', 'rb') as f:
#     arr.fromfile(f, 5)
# print(arr) # 数组和C结构体类型可以直接写入bin文件，读取的时候也可以直接从bin文件加载

# StringIO()提供像操作文件一样的接口，但是数据保存在内存中，而不是文件
# 但是StringIO()并没有真正的文件描述符，无法用在套接字，管道等系统级文件中
# from io import StringIO
#
# s = StringIO()
# print('Hello World', file=s)
# s.write('new content')
# s.write('new content')
# s.write('new content')
# print(s.getvalue())
# s.seek(0)
# str = s.read(10)
# print(str)
#


# 可以每次只从文件中读取固定长度的数据 partial可以实现
# from functools import partial
#
# RECORD_SIZE = 32
#
# with open('somefile.data', 'rb') as f:
#     records = iter(partial(f.read, RECORD_SIZE), b'')
#     for r in records:

# 读取二进制文件到可变缓存区
# import os.path
#
#
# def read_into_buffer(filename):
#     buf = bytearray(os.path.getsize(filename))
#     with open(filename, 'rb') as f:
#         f.readinto(buf)
#     return buf
#
#
# buf = read_into_buffer('io.bin')
# print(buf)

# 文件路径名字操作 基本上的路径都用os.path模块来管理，尽量不要自己使用字符串手搓
# import os.path
#
# print(os.path.abspath('.'))
# print(os.path.isabs('.'))
# path = '/Users/beazley/Data/data.csv'
# os.path.join('tmp', 'data', os.path.basename(path))
# os.path.isfile()判断文件是否存在
# names = os.listdir('somedir')获取文件列表，包括所有文件和子目录


# 序列化python对象
import pickle
import time
import threading
